﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Common.Silverlight;
using SMS.Controls.CategorySVC;
using SMS.Controls.CitySVC;
using SMS.Entities;

namespace SMS.Model
{
    public class CityMasterModel
    {
        ServiceProxy<CityServiceClient> proxy = null;
        public event Action<int> OnSaveCityCompleted;
        public event Action<System.Collections.ObjectModel.ObservableCollection<Entities.City>> OnSearchCompleted;
        public event Action<SMSException> OnServiceError;
        public CityMasterModel()
        {
            proxy = new ServiceProxy<CityServiceClient>("Service/CityService.svc");
            proxy.Client.SaveCityCompleted += new EventHandler<SaveCityCompletedEventArgs>(Client_SaveCityCompleted);
            proxy.Client.SearchCityCompleted += new EventHandler<SearchCityCompletedEventArgs>(Client_SearchCityCompleted);
        }

        void Client_SearchCityCompleted(object sender, SearchCityCompletedEventArgs e)
        {
            if (OnSearchCompleted != null)
                OnSearchCompleted(e.Result);
        }

        void Client_SaveCityCompleted(object sender, SaveCityCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                if (OnSaveCityCompleted != null)
                    OnSaveCityCompleted(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);
               
        }

        public void SearchCity(string searchText)
        {
            proxy.Client.SearchCityAsync(App.LoggedinUserName, searchText);
        }

        public void SaveCity(City city)
        {
            proxy.Client.SaveCityAsync(App.LoggedinUserName,city);
        }
    }
}
